<template>
  <el-dialog
    ref="refDlg"
    v-el-drag-dialog
    :title="`${(detailData && detailData.id) ? '编辑' : '新增'}类目`"
    :visible.sync="dialogShow"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :before-close="clearForm"
    width="600px"
    append-to-body
    @close="closeDialog"
  >
    <div>
      <el-form ref="refForm" :rules="formRules" :model="formData" size="mini" label-width="100px">
        <el-row>
          <el-col :span="24">
            <el-form-item
              label="模块编码:"
              prop="classifyCode"
              :rules="[
                { required: !(detailData&&detailData.id), message: '请输入编码', trigger: 'blur' }
              ]"
            >
              <el-input v-model.trim="formData.classifyCode" :disabled="!!(detailData&&detailData.id)" maxlength="20" placeholder="请输入编码" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="模块名称:" prop="classifyName">
              <el-input v-model.trim="formData.classifyName" maxlength="20" placeholder="请输入名称" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div slot="footer">
      <el-button size="mini" @click="closeDialog">取消</el-button>
      <el-button v-preventReClick v-loading="submiting" size="mini" type="primary" @click="submitForm">保存</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { saveModular } from '@/api/modular'
export default {
  name: 'HelpModularEdit',
  props: {
    visible: {
      type: Boolean,
      default: false,
      required: true
    },
    detailData: {
      type: [String, Object],
      default: ''
    }
  },
  data() {
    return {
      submiting: false,
      formData: {
        classifyName: '',
        classifyCode: ''
      },
      formRules: {
        classifyName: [
          { required: true, message: '请输入名称', trigger: 'blur' }
        ]
      }
    }
  },

  computed: {
    dialogShow: {
      get() {
        return this.visible
      },
      set(val) {
        this.$emit('update:visible', val)
      }
    }
  },
  created() {
    const data = this.detailData
    if (data) {
      this.formData = {
        classifyName: data.classifyName,
        classifyCode: data.classifyCode
      }
    }
  },

  methods: {
    closeDialog() {
      this.dialogShow = false
      this.clearForm()
    },
    clearForm(done) {
      this.formData = {
        classifyName: '',
        classifyCode: ''
      }
      this.$refs.refForm && this.$refs.refForm.clearValidate()
      done && done()
    },
    // 提交表单处理
    submitForm() {
      if (this.submiting) return
      this.$refs['refForm'].validate((valid) => {
        if (valid) {
          const tmp = {}
          if (this.detailData.id) {
            tmp.id = this.detailData.id
          }
          const params = { ...this.formData, ...tmp }
          this.submiting = true
          saveModular(params).then(res => {
            this.submiting = false
            if (res.success === 1) {
              this.$message({
                message: res.msg || '保存成功',
                type: 'success'
              })
              this.$emit('success', !params.id)
              this.closeDialog()
            }
          }).catch(() => {
            this.submiting = false
          })
        }
      })
    }
  }
}
</script>
<style lang='scss' scoped>
  .img-upload-container {
    width: fit-content;
    display: flex;
    flex-wrap: wrap;
    justify-content: flex-end;
    align-items: center;
    .icon-msg{
      margin-left: 10px;
      color: #999;
    }
  }
</style>
